$(function () {
    obj.init();
});

window.obj = new function() {
    'use strict';
    return {
        groupGrid: null,
        dataGrid: null,
        params: {
            schoolId: null
        },
        dom: {
            groupAdd: null,
            studAdd: null,
            groupDel: null
        },
        init: function () {
            this.params.schoolId = xhTool.getQuery('schoolId');

            this.dom.groupAdd = $('#bt_group_add');
            this.dom.groupDel = $('#bt_group_del');
            this.dom.studAdd  = $('#bt_stud_add');

            this.groupGrid  = $('#group_list');
            this.dataGrid   = new xhDataGrid($('#list'), 'stats_brush/getBrushGroupUser', {schoolId: this.params.schoolId});

            var $this = this;
            this.groupGrid.treegrid({
                url: baseUrl + 'stats_brush/getClassBrushGroup',
                queryParams: {schoolId: this.params.schoolId},
                // onBeforeExpand: this.funObj().onbeforeExpand,
                onSelect: function (row){
                    $this.funObj().groupGridSelect(row);
                },
                onLoadSuccess: function (row, data) {
                    $this.funObj().groupLoadSuccess(row, data);
                },
                onDblClickRow: function (row) {
                    $this.funObj().groupDblClick(row);
                }
            });

            this.dataGrid.onLoadSuccess = function (data) {$this.dataGrid.clear();};
            this.dataGrid.parseToolbar().init().activeBtn($('#bt_stud_del'));

            xhExtra.disableButton(this.dom.groupAdd.add(this.dom.groupDel).add(this.dom.studAdd), 'eu');

            this.listener();
        },
        listener: function () {
            var $this = this;
            //新增组
            $this.dom.groupAdd.click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addBrushGroup();
            });
            //新增学生
            $this.dom.studAdd.click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addBrushGroupUser();
            });
            //删除学生
            $('#bt_stud_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delBrushGroupUser();
            });
            //删除组
            $this.dom.groupDel.click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delBrushGroup();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //班级下无数据则改为不可展开
                groupLoadSuccess: function (row, data) {
                    if ($.isEmptyObject(data) && row && row.target === 'class') {
                        $this.groupGrid.treegrid('update', {id: row.id, row: {state: 'open'}});
                    }
                    $('.tip').tooltip({
                        position: 'bottom',
                        content: '<span style="color:#fff">双击编辑</span>',
                        onShow: function(){
                            $(this).tooltip('tip').css({
                                backgroundColor: '#666',
                                borderColor: '#666'
                            });
                        }
                    });
                },
                groupGridSelect: function (row) {
                    $this.dataGrid.setTitle().command('loadData', []);
                    xhExtra.disableButton($this.dom.groupAdd.add($this.dom.studAdd).add($this.dom.groupDel), 'eu');
                    //选中班级后激活添加按钮，清除学生表
                    if (row.target === 'class') {
                        xhExtra.enableButton($this.dom.groupAdd, 'eu');
                        return false;
                    }
                    //选中组则刷新学生列表
                    if (row.target === 'group') {
                        xhExtra.enableButton($this.dom.studAdd.add($this.dom.groupDel), 'eu');
                        $this.dataGrid.setTitle(null, '-' + row.name).reload({groupId: row.targetId});
                        return false;
                    }
                },
                //双击组更新
                groupDblClick: function (row) {
                    if (!row || row.target !== 'group') {
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '编辑组-' + row.name,
                        content: xhTool.getFrameString('stats_brush/brushGroupForm', {groupId: row.targetId}, 450),
                        size: 'md',
                        button: '更新'
                    });
                },
                //新增组
                addBrushGroup: function () {
                    var row = obj.groupGrid.treegrid('getSelected');
                    if (!row || row.target !== 'class') {
                        window.top.xhTop.showError('未选中班级！');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '新增组-' + row.name,
                        content: xhTool.getFrameString('stats_brush/brushGroupForm', {schoolId: $this.params.schoolId, classId: row.targetId}, 450),
                        size: 'md',
                        button: '保存'
                    });
                },
                addBrushGroupUser: function () {
                    var row = obj.groupGrid.treegrid('getSelected');
                    if (!row || row.target !== 'group') {
                        window.top.xhTop.showError('未选中分组！');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '增加学生-' + row.name,
                        content: xhTool.getFrameString('stats_brush/brushGroupUserAdd', {schoolId: $this.params.schoolId, classId: row.id.split('_')[0], groupId: row.targetId}, 500),
                        size: 'md',
                        button: '新增'
                    });
                },
                delBrushGroupUser: function () {
                    var ids = $this.dataGrid.getSelectValues('id');
                    if (!ids) {
                        window.top.xhTop.showError('无选中用户！');
                        return false;
                    }
                    xhExtra.askChangeStatus('stats_brush/delBrushGroupUser', {ids: ids}, $this.dataGrid, '确认删除？');
                },
                delBrushGroup: function () {
                    var row = obj.groupGrid.treegrid('getSelected');
                    if (!row || row.target !== 'group') {
                        window.top.xhTop.showError('未选中分组！');
                        return false;
                    }
                    xhExtra.askChangeStatus('stats_brush/delBrushGroup', {ids: [row.targetId]}, null, '确认删除？', function () {
                        // $this.groupGrid.treegrid('reload', row.id.split('_')[0]);
                        $this.funObj().reloadGroupGrid(row.id.split('_')[0]);
                    });
                },
                reloadGroupGrid: function (classId) {
                    $this.groupGrid.treegrid('update', {id: classId, row: {state: 'closed'}}).treegrid('reload', classId);
                }
                // onbeforeExpand: function (row, param) {
                //     console.log(row);
                //     console.log($this.groupGrid.treegrid('options'));
                //     $this.groupGrid.treegrid('options').url         = baseUrl + 'stats_brush/getClassBrushGroup';
                //     $this.groupGrid.treegrid('options').queryParams = {classId: row.id};
                // }
            }
        }
    }
};